package smedi.ismedi.web.bff.controller.labor;

import com.smedi.ismedi.labor.api.dto.request.*;
import com.smedi.ismedi.labor.api.dto.response.*;
import com.smedi.ismedi.labor.api.feign.RemoteLaborCoreFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import smedi.ismedi.web.bff.controller.BaseController;
import smedi.ismedi.web.bff.infrastructure.constant.CustomError;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 劳务人员核心接口
 *
 * @author xulinjun
 */
@Slf4j
@RestController
@Api(tags = {"人员管理接口"})
@RequestMapping("/webbff/v1/laborCore")
@AllArgsConstructor
public class LaborCoreController extends BaseController {
    private final RemoteLaborCoreFeignApi remoteLaborCoreFeignApi;

    /**
     * 下载文件输出
     *
     * @param httpServletResponse
     * @param fileName
     * @param bytes
     * @throws IOException
     */
    private void writeHttpServletResponse(HttpServletResponse httpServletResponse, String fileName, byte[] bytes) throws IOException {
        httpServletResponse.reset();
        httpServletResponse.setContentType("application/x-download");
        httpServletResponse.addHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
        httpServletResponse.getOutputStream().write(bytes);
        httpServletResponse.getOutputStream().close();
    }

    /**
     * 返场
     *
     * @param laborProjectAssignmentRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "返场")
    @PostMapping(value = "/back")
    ResponseData<LaborProjectAssignmentResponse> postBack(@Validated @RequestBody LaborProjectAssignmentRequest laborProjectAssignmentRequest) {
        return remoteLaborCoreFeignApi.postBack(laborProjectAssignmentRequest);
    }

    /**
     * 保存劳务人员实名认证后的结果
     *
     * @param laborAuthenticationRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "保存劳务人员实名认证后的结果")
    @PostMapping(value = "/saveLaborAuthentication")
    public ResponseData<LaborBasicResponse> postSaveLaborAuthentication(@Validated @RequestBody LaborAuthenticationRequest laborAuthenticationRequest) {
        return remoteLaborCoreFeignApi.postSaveLaborAuthentication(laborAuthenticationRequest);
    }

    /**
     * 在移动端下根据项目id查询在场的劳务人员
     *
     * @param offset
     * @param pageSize
     * @param projectId
     * @param lastName
     * @param team
     * @param gender
     * @param overAge
     * @param highAge
     * @param hasSpecialJob
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "在移动端下根据项目id查询在场的劳务人员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "offset", value = "页码", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "lastName", value = "姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "team", value = "部门/班组", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "gender", value = "性别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "epcTypeMeaning", value = "总承包模式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "overAge", value = "超龄", dataType = "Boolean", dataTypeClass = Boolean.class),
            @ApiImplicitParam(paramType = "query", name = "highAge", value = "高龄", dataType = "Boolean", dataTypeClass = Boolean.class),
            @ApiImplicitParam(paramType = "query", name = "hasSpecialJob", value = "是否包含特殊工种", dataType = "Boolean", dataTypeClass = Boolean.class)
    })
    @GetMapping(value = "/queryLaborEffectiveProjectAssignmentMobilePagination")
    ResponseData<PaginationData<LaborAssignmentDetailResponse>> getQueryLaborEffectiveProjectAssignmentMobilePagination(@RequestParam("offset") Long offset,
                                                                                                                        @RequestParam("pageSize") Long pageSize,
                                                                                                                        @RequestParam(value = "projectId", required = false) Long projectId,
                                                                                                                        @RequestParam(value = "lastName", required = false) String lastName,
                                                                                                                        @RequestParam(value = "team", required = false) String team,
                                                                                                                        @RequestParam(value = "gender", required = false) String gender,
                                                                                                                        @RequestParam(value = "epcTypeMeaning", required = false) String epcTypeMeaning,
                                                                                                                        @RequestParam(value = "assignmentStatus", required = false) String assignmentStatus,
                                                                                                                        @RequestParam(value = "overAge", required = false) boolean overAge,
                                                                                                                        @RequestParam(value = "highAge", required = false) boolean highAge,
                                                                                                                        @RequestParam(value = "hasSpecialJob", required = false) boolean hasSpecialJob) {
        return remoteLaborCoreFeignApi.getQueryLaborEffectiveProjectAssignmentMobilePagination(offset, pageSize, projectId, lastName, team, gender, epcTypeMeaning, assignmentStatus, overAge, highAge, hasSpecialJob);
    }
    /**
     * 在移动端下根据项目id查询在场的劳务人员
     *

     * @param projectId
     * @param lastName
     * @param team
     * @param gender
     * @param overAge
     * @param highAge
     * @param hasSpecialJob
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "在移动端下根据项目id查询在场的劳务人员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "lastName", value = "姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "team", value = "部门/班组", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "gender", value = "性别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "epcTypeMeaning", value = "总承包模式", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "overAge", value = "超龄", dataType = "Boolean", dataTypeClass = Boolean.class),
            @ApiImplicitParam(paramType = "query", name = "highAge", value = "高龄", dataType = "Boolean", dataTypeClass = Boolean.class),
            @ApiImplicitParam(paramType = "query", name = "subcontractor", value = "分包单位名称", dataType = "String", dataTypeClass = Boolean.class),
            @ApiImplicitParam(paramType = "query", name = "hasSpecialJob", value = "是否包含特殊工种", dataType = "Boolean", dataTypeClass = Boolean.class)
    })
    @GetMapping(value = "/queryLaborEffectiveProjectAssignment")
    ResponseData<List<LaborAssignmentDetailResponse>> getQueryLaborEffectiveProjectAssignment(@RequestParam(value = "projectId", required = false) Long projectId,
                                                                                              @RequestParam(value = "lastName", required = false) String lastName,
                                                                                              @RequestParam(value = "team", required = false) String team,
                                                                                              @RequestParam(value = "gender", required = false) String gender,
                                                                                              @RequestParam(value = "epcTypeMeaning", required = false) String epcTypeMeaning,
                                                                                              @RequestParam(value = "overAge", required = false) boolean overAge,
                                                                                              @RequestParam(value = "highAge", required = false) boolean highAge,
                                                                                              @RequestParam(value = "subcontractor", required = false) String projectOrganizationFullName,
                                                                                              @RequestParam(value = "hasSpecialJob", required = false) boolean hasSpecialJob){
        return remoteLaborCoreFeignApi.getQueryLaborEffectiveProjectAssignment(projectId,lastName,team,gender,epcTypeMeaning,overAge,highAge,projectOrganizationFullName,hasSpecialJob);
    }


    /**
     * PC端下查询劳务人员项目分配信息
     *
     * @param offset
     * @param pageSize
     * @param laborProjectAssignmentPcQueryRequest
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "PC端下查询劳务人员项目分配信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "offset", value = "页码", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryLaborEffectiveProjectAssignmentPcPagination")
    public ResponseData<PaginationData<LaborAssignmentDetailResponse>> getQueryLaborProjectAssignmentPcPagination(@RequestParam("offset") Long offset,
                                                                                                                  @RequestParam("pageSize") Long pageSize,
                                                                                                                  @SpringQueryMap LaborProjectAssignmentPcQueryRequest laborProjectAssignmentPcQueryRequest) {
        return remoteLaborCoreFeignApi.getQueryLaborProjectAssignmentPcPagination(offset, pageSize, laborProjectAssignmentPcQueryRequest);
    }

    /**
     * PC端下导出劳务人员项目分配信息
     *
     * @param laborProjectAssignmentPcQueryRequest
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "PC端下导出劳务人员项目分配信息")
    @GetMapping(value = "/downloadLaborEffectiveProjectAssignmentExcel")
    public void getDownloadLaborEffectiveProjectAssignmentExcel(@SpringQueryMap LaborProjectAssignmentPcQueryRequest laborProjectAssignmentPcQueryRequest, HttpServletResponse httpServletResponse) throws IOException {
        ResponseData<ReportExcelResponse> responseData = remoteLaborCoreFeignApi.getDownloadLaborEffectiveProjectAssignmentExcel(laborProjectAssignmentPcQueryRequest);
        if (responseData.isSuccess()) {
            writeHttpServletResponse(httpServletResponse, responseData.getData().getFileName(), responseData.getData().getBytes());
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
    }

    /**
     * 批量为劳务人员办理离场
     *
     * @param assignmentIds
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "批量为劳务人员办理离场")
    @PostMapping(value = "/batchLeaveProject")
    public ResponseData<Boolean> postBatchLeaveProject(@RequestBody List<Long> assignmentIds) {
        return remoteLaborCoreFeignApi.postBatchLeaveProject(assignmentIds);
    }

    /**
     * 为单个劳务人员办理离场
     *
     * @param laborProjectAssignmentRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "为单个劳务人员办理离场")
    @PostMapping(value = "/leaveProject")
    public ResponseData<Boolean> postLeaveProject(@RequestBody LaborProjectAssignmentRequest laborProjectAssignmentRequest) {
        return remoteLaborCoreFeignApi.postLeaveProject(laborProjectAssignmentRequest);
    }

    /**
     * 根据劳务人员id获取劳务人员详细信息
     *
     * @param laborId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据劳务人员id获取劳务人员详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "laborId", value = "劳务人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryLaborDetailInfo")
    public ResponseData<LaborDetailResponse> getQueryLaborDetailInfo(@RequestParam("laborId") Long laborId) {
        return remoteLaborCoreFeignApi.getQueryLaborDetailInfo(laborId);
    }

    /**
     * 修改劳务人员手机号、银行类型、银行卡
     *
     * @param laborBasicInfoModifyRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "修改劳务人员手机号、银行类型、银行卡")
    @PostMapping(value = "/saveLaborBasicInfoModify")
    public ResponseData<LaborBasicResponse> postSaveLaborBasicInfoModify(@Validated @RequestBody LaborBasicInfoModifyRequest laborBasicInfoModifyRequest) {
        return remoteLaborCoreFeignApi.postSaveLaborBasicInfoModify(laborBasicInfoModifyRequest);
    }

    /**
     * 根据分配id获取劳务人员的项目分配记录
     *
     * @param assignmentId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据分配id获取劳务人员的项目分配记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "assignmentId", value = "项目分配id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryLaborProjectAssignmentByAssignmentId")
    public ResponseData<LaborProjectAssignmentResponse> getQueryLaborProjectAssignmentByAssignmentId(@RequestParam("assignmentId") Long assignmentId) {
        return remoteLaborCoreFeignApi.getQueryLaborProjectAssignmentByAssignmentId(assignmentId);
    }

    /**
     * 修改劳务人员项目分配信息
     *
     * @param laborProjectAssignmentModifyRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "修改劳务人员项目分配信息")
    @PostMapping(value = "/saveLaborProjectAssignmentModify")
    public ResponseData<LaborProjectAssignmentResponse> postSaveLaborProjectAssignmentModify(@RequestBody LaborProjectAssignmentModifyRequest laborProjectAssignmentModifyRequest) {
        return remoteLaborCoreFeignApi.postSaveLaborProjectAssignmentModify(laborProjectAssignmentModifyRequest);
    }

    /**
     * 导入设置分页查询
     * @param LaborProjectImportConfigPageRequest
     * @return esponseData<PaginationData<LaborProjectImportConfigResponse>>
     */
    @ApiOperation(value = "导入设置分页查询", nickname = "导入设置分页查询")
    @PostMapping(value = "/queryLaborProjectImportConfigPage")
    ResponseData<PaginationData<LaborProjectImportConfigResponse>> queryLaborProjectImportConfigPage(@RequestBody LaborProjectImportConfigPageRequest LaborProjectImportConfigPageRequest){
        return remoteLaborCoreFeignApi.queryLaborProjectImportConfigPage(LaborProjectImportConfigPageRequest);
    }

    /**
     * 查询当前用户可操作项目列表查询
     * @return ResponseData<List<LaborProjectImportConfigResponse>>
     */
    @ApiOperation(value = "查询当前用户可操作项目列表查询", nickname = "查询当前用户可操作项目列表查询")
    @PostMapping(value = "/queryLaborProjectImportConfigList")
    ResponseData<List<LaborProjectImportConfigResponse>> queryLaborProjectImportConfigList(){
        return remoteLaborCoreFeignApi.queryLaborProjectImportConfigList();
    }

    /**
     * 导入设置根据id查询详情
     * @param configIds
     * @return ResponseData<List<LaborProjectImportConfigResponse>>
     */
    @ApiOperation(value = "导入设置根据id查询详情", nickname = "导入设置根据id查询详情")
    @PostMapping(value = "/queryLaborProjectImportConfigByIds")
    ResponseData<List<LaborProjectImportConfigResponse>> queryLaborProjectImportConfigByIds(@RequestBody List<Long> configIds){
        return remoteLaborCoreFeignApi.queryLaborProjectImportConfigByIds(configIds);
    }


    /**
     * 删除单个导入设置
     * @param configId
     * @return ResponseData<Boolean>
     */
    @ApiOperation(value = "删除单个导入设置", nickname = "删除单个导入设置")
    @PostMapping(value = "/delLaborProjectImportConfigById")
    ResponseData<Boolean> delLaborProjectImportConfigById(@RequestParam(value = "configId", required = true) Long configId){
        return remoteLaborCoreFeignApi.delLaborProjectImportConfigById(configId);
    }

    /**
     * 保存导入设置
     * @param saveLaborProjectImportConfigRequest 参数
     * @return
     */
    @ApiOperation(value = "保存导入设置", nickname = "保存导入设置")
    @PostMapping(value = "/saveLaborProjectImportConfig")
    ResponseData<LaborProjectImportConfigResponse> saveLaborProjectImportConfig(@RequestBody SaveLaborProjectImportConfigRequest saveLaborProjectImportConfigRequest){
        return remoteLaborCoreFeignApi.saveLaborProjectImportConfig(saveLaborProjectImportConfigRequest);
    }


    /**
     * 通过文件导入更新劳务人员信息
     * @param file 文件
     * @param projectId 项目id
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "通过文件导入更新劳务人员信息", nickname = "通过文件导入更新劳务人员信息", httpMethod = "POST")
    @PostMapping(value = "/updateLaborByExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseData<List<ImportExcelResultResponse>> updateLaborByExcel(@ApiParam(value = "file", required = true) @RequestPart(value = "file") MultipartFile file,
                                                                     @RequestParam(value = "projectId", required = true) Long projectId) throws IOException{
        return remoteLaborCoreFeignApi.updateLaborByExcel(file,projectId);
    }

    /**
     * 劳务人员信息模板导出
     */
    @ApiOperation(value = "劳务人员信息模板导出", nickname = "劳务人员信息模板导出")
    @PostMapping(value = "/exportLaborTemplate")
    ResponseEntity<byte[]> exportLaborTemplate(){
        return remoteLaborCoreFeignApi.exportLaborTemplate();
    }
}
